package com.wollo.utils;

import com.wollo.domain.Check;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author OuYang
 * @date 2024/9/1720:37
 * @description
 */
public class ExcelUtils {
    //导入用户的考勤信息数据
    public static  void exportExcel(HttpServletResponse response, String fileName, List<Check> list) throws IOException {
        // 创建新的Excel 工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 在Excel工作簿中建一工作表，其名为缺省值
        // 如要新建一名为"效益指标"的工作表，其语句为：
        // HSSFSheet sheet = workbook.createSheet("sheet1");
        HSSFSheet sheet = workbook.createSheet("考勤信息");
        //创建表头行
        HSSFRow row = sheet.createRow((short)0);//相当于创建第0行数据
        //创建自定义字体
        HSSFFont font = workbook.createFont();
        font.setColor(HSSFFont.COLOR_RED);//设置颜色
        font.setBold(true);//设置粗体
        //设置单元格的样式
        HSSFCellStyle cellStyle= workbook.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//设置单元格背景色
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置单元格背景色填充样式
        //在索引0的位置创建单元格（第一列）
        HSSFCell cell1 = row.createCell((short) 0);
        cell1.setCellType(CellType.STRING); //设置单元格的内容格式
        cell1.setCellValue("签到ID"); //设置单元格的内容
        cell1.setCellStyle(cellStyle);
        HSSFCell cell2 = row.createCell((short) 1);
        cell2.setCellType(CellType.STRING);
        cell2.setCellValue("签到日期");
        cell2.setCellStyle(cellStyle);
        HSSFCell cell3 = row.createCell((short) 2);
        cell3.setCellType(CellType.STRING);
        cell3.setCellValue("签到时间");
        cell3.setCellStyle(cellStyle);
        HSSFCell cell4 = row.createCell((short) 3);
        cell4.setCellType(CellType.STRING);
        cell4.setCellValue("签到状态");
        cell4.setCellStyle(cellStyle);
        HSSFCell cell5 = row.createCell((short) 4);
        cell5.setCellType(CellType.STRING);
        cell5.setCellValue("签到结果");
        cell5.setCellStyle(cellStyle);
        HSSFCell cell6 = row.createCell((short) 5);
        cell6.setCellType(CellType.STRING);
        cell6.setCellValue("备注");
        cell6.setCellStyle(cellStyle);
        //创建数据行
        for(int i=0;i<list.size();i++){
            //获取行数据
            Check e = list.get(i);
            //创建行
            //因为表头占了一行，所以要加1
            HSSFRow r = sheet.createRow((short)i+1);
            //创建行的单元格
            HSSFCell c1 = r.createCell((short) 0);
            c1.setCellType(CellType.STRING); //设置单元格的内容格式
            c1.setCellValue(e.getCheckId()); //设置单元格的内容
            HSSFCell c2 = r.createCell((short) 1);
            c2.setCellType(CellType.STRING);
            c2.setCellValue(e.getCheckDate().toString());
            HSSFCell c3 = r.createCell((short) 2);
            c3.setCellType(CellType.STRING);
            c3.setCellValue(e.getCheckTime().toString());
            HSSFCell c4 = r.createCell((short) 3);
            c4.setCellType(CellType.STRING);
            if(e.getCheckStatus().equals("1")){
                c4.setCellValue("早上签到");
            }else if(e.getCheckStatus().equals("2")){
                c4.setCellValue("中午签到");
            }else{
                c4.setCellValue("晚上签到");
            }
            HSSFCell c5 = r.createCell((short) 4);
            c5.setCellType(CellType.STRING);
            if(e.getCheckResult().equals("1")){
                c5.setCellValue("正常");
            }else{
                c5.setCellValue("迟到");
            }
            HSSFCell c6 = r.createCell((short) 5);
            c6.setCellType(CellType.STRING);
            c6.setCellValue(e.getRemark());
        }
        //响应结果
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");//设置响应头信息
        response.setCharacterEncoding("utf-8");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
    }

}
